import cv2 
import numpy as np
import matplotlib.pyplot as plt

# 安装 skimage
# conda 未换源 conda install -c anaconda scikit-image
# conda 已换源 conda install scikit-image
# pip install scikit-image

# 导入skimage
from skimage.feature import hog
from skimage import data, exposure

# 读取图像
img = cv2.imread('./test_imgs/cat1.jpg')
img = cv2.resize(img,(200,370))

img_fixed = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# https://scikit-image.org/docs/dev/api/skimage.feature.html?highlight=hog#skimage.feature.hog
fd,hog_image = hog(image=img_gray,orientations=9,pixels_per_cell=(8,8),cells_per_block=(2,2),visualize=True)
# image 输入图像
# orientations 把180度分成几份， bin的数量
# pixels_per_cell : 元组形式，一个Cell内的像素大小
# cells_per_block : 元组形式，一个Block内的Cell数量
# visualize ： 是否需要可视化，如果True，hog会返回numpy图像

fig, (ax1,ax2) = plt.subplots(1,2,figsize=(20,10),sharex=True,sharey=True)
ax1.axis('off')
ax1.imshow(img_fixed)
ax1.set_title('Input image')

# rescale histogram for better display
hog_img_rescaled = exposure.rescale_intensity(hog_image,in_range=(0,10))

ax2.axis('off')
ax2.imshow(hog_img_rescaled,cmap=plt.cm.gray)

# 查看一下HOG特征大小
fd.shape()
fd